package com.sjb.sixteen_journey_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sjb.sixteen_journey_backend.common.CommonHandle;
import com.sjb.sixteen_journey_backend.common.R;
import com.sjb.sixteen_journey_backend.entity.*;
import com.sjb.sixteen_journey_backend.mapper.*;
import com.sjb.sixteen_journey_backend.service.ICommentService;
import com.sjb.sixteen_journey_backend.service.IShowTicketService;
import com.sjb.sixteen_journey_backend.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 晒单表 服务实现类
 * </p>
 *
 * @author jcz
 * @since 2024-07-13
 */
@Service
public class ShowTicketServiceImpl  implements IShowTicketService {

    @Autowired
    private ShowTicketMapper showTicketMapper;

    @Autowired
    private AirTicketMapper airTicketMapper;

    @Autowired
    private HotelTicketMapper hotelTicketMapper;

    @Autowired
    private InsuranceTicketMapper insuranceTicketMapper;

    @Autowired
    private OthersTicketMapper othersTicketMapper;


    @Autowired
    private CommonHandle commonHandle;

    @Autowired
    private ICommentService commentService;

    @Autowired
    private FollowerMapper followerMapper;

    @Override
    public <T> R<List<T>> showAllTicket() {
        try {
            Map<String, Object> m = ThreadLocalUtil.get();
            Integer userId = (Integer) m.get("id");
            List<T> tickets = new ArrayList<>();
            List<AirTicket> airTicket = showTicketMapper.selectAirTicket(userId);
            List<HotelTicket> hotelTicket = showTicketMapper.selectHotelTicket(userId);
            List<InsuranceTicket> insuranceTicket = showTicketMapper.selectInsuranceTicket(userId);
            List<OthersTicket> othersTicket = showTicketMapper.selectOthersTicket(userId);

            //将字符串图片地址转换为数组
            for (AirTicket myTime : airTicket) {
                List<String> strings = commonHandle.imageByStringToList(myTime.getPath());
                myTime.setImages(strings);
                QueryWrapper<Comment> query = Wrappers.query();
                query.eq("module_status",myTime.getStatus());
                query.eq("module_id", myTime.getAirTicketId());
                long count = commentService.count(query);
                myTime.setCommentCount(count> 0 ? count : 0);
            }
            for (HotelTicket myTime : hotelTicket) {
                commonHandle.imageByStringToList(myTime.getPath());
                List<String> strings = commonHandle.imageByStringToList(myTime.getPath());
                myTime.setImages(strings);
                QueryWrapper<Comment> query = Wrappers.query();
                query.eq("module_status",myTime.getStatus());
                query.eq("module_id", myTime.getHotelTicketId());
                long count = commentService.count(query);
                myTime.setCommentCount(count> 0 ? count : 0);
            }
            for (InsuranceTicket myTime : insuranceTicket) {
                commonHandle.imageByStringToList(myTime.getPath());
                List<String> strings = commonHandle.imageByStringToList(myTime.getPath());
                myTime.setImages(strings);
                QueryWrapper<Comment> query = Wrappers.query();
                query.eq("module_status",myTime.getStatus());
                query.eq("module_id", myTime.getInsuranceTicketId());
                long count = commentService.count(query);
                myTime.setCommentCount(count> 0 ? count : 0);
            }
            for (OthersTicket myTime : othersTicket) {
                commonHandle.imageByStringToList(myTime.getPath());
                List<String> strings = commonHandle.imageByStringToList(myTime.getPath());
                myTime.setImages(strings);
                QueryWrapper<Comment> query = Wrappers.query();
                query.eq("module_status",myTime.getStatus());
                query.eq("module_id", myTime.getOthersTicketId());
                long count = commentService.count(query);
                myTime.setCommentCount(count> 0 ? count : 0);
            }

            Map<String, List<T>> map = new HashMap<>();
            map.put("AirTicket", (List<T>) airTicket);
            map.put("HotelTicket", (List<T>) hotelTicket);
            map.put("InsuranceTicket", (List<T>) insuranceTicket);
            map.put("OthersTicket", (List<T>) othersTicket);
            tickets.add((T) map);
            return R.success(tickets, "查询所有晒单成功");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public R showAllTicketByIdAndStatus(Integer status, Integer id,Boolean f) {
        try {
            Map<String, Object> m = ThreadLocalUtil.get();
            Integer userId = (Integer) m.get("id");
            if (status == 1) {
                AirTicket airTicketDto = showTicketMapper.selectAirTicketById(id, userId);
                if(airTicketDto==null) return R.error("晒单id不正确！", 400);
                if(f){
                    //每访问一下接口，其实浏览量就增加一次
                    UpdateWrapper<AirTicket> wrapper = Wrappers.update();
                    wrapper.set("pv", airTicketDto.getPv() + 1);
                    wrapper.eq("air_ticket_id", id);
                    airTicketMapper.update(null, wrapper);
                    airTicketDto = showTicketMapper.selectAirTicketById(id, userId);
                }
                List<String> strings = commonHandle.imageByStringToList(airTicketDto.getPath());// 设置图片列表
                airTicketDto.setImages(strings);
                QueryWrapper<Comment> query = Wrappers.query();
                query.eq("module_status",status);
                query.eq("module_id", airTicketDto.getAirTicketId());
                long count = commentService.count(query);
                airTicketDto.setCommentCount(count> 0 ? count : 0);
                QueryWrapper<Follower> followerQueryWrapper = Wrappers.query();
                followerQueryWrapper.eq("user_id",userId);
                followerQueryWrapper.eq("fans_id", airTicketDto.getUserId());
                Follower follower = followerMapper.selectOne(followerQueryWrapper);
                airTicketDto.setIs_followedByUser(follower==null ? false :follower.getStatus());

                return R.success(airTicketDto, "查询成功");
            } else if (status == 2) {
                HotelTicket hotelTicketDto = showTicketMapper.selectHotelTicketById(id, userId);
                if(hotelTicketDto==null) return R.error("晒单id不正确！", 400);
                if(f){
                    //每访问一下接口，其实浏览量就增加一次
                    UpdateWrapper<HotelTicket> wrapper = Wrappers.update();
                    wrapper.set("pv", hotelTicketDto.getPv() + 1);
                    wrapper.eq("hotel_ticket_id", id);
                    hotelTicketMapper.update(null, wrapper);
                }


                hotelTicketDto = showTicketMapper.selectHotelTicketById(id, userId);
                List<String> strings = commonHandle.imageByStringToList(hotelTicketDto.getPath());// 设置图片列表
                hotelTicketDto.setImages(strings);
                QueryWrapper<Comment> query = Wrappers.query();
                query.eq("module_status",status);
                query.eq("module_id", hotelTicketDto.getHotelTicketId());
                long count = commentService.count(query);
                hotelTicketDto.setCommentCount(count> 0 ? count : 0);
                QueryWrapper<Follower> followerQueryWrapper = Wrappers.query();
                followerQueryWrapper.eq("user_id",userId);
                followerQueryWrapper.eq("fans_id", hotelTicketDto.getUserId());
                Follower follower = followerMapper.selectOne(followerQueryWrapper);
                hotelTicketDto.setIs_followedByUser(follower==null ? false :follower.getStatus());

                return  R.success(hotelTicketDto, "查询成功");
            } else if (status == 3) {
                InsuranceTicket insuranceTicketDto = showTicketMapper.selectInsuranceTicketById(id, userId);
                if(insuranceTicketDto==null) return R.error("晒单id不正确！", 400);
                if(f){
                    //每访问一下接口，其实浏览量就增加一次
                    UpdateWrapper<InsuranceTicket> wrapper = Wrappers.update();
                    wrapper.set("pv", insuranceTicketDto.getPv() + 1);
                    wrapper.eq("insurance_ticket_id", id);
                    insuranceTicketMapper.update(null, wrapper);
                    insuranceTicketDto = showTicketMapper.selectInsuranceTicketById(id, userId);
                }

                List<String> strings = commonHandle.imageByStringToList(insuranceTicketDto.getPath());// 设置图片列表
                insuranceTicketDto.setImages(strings);
                QueryWrapper<Comment> query = Wrappers.query();
                query.eq("module_status",status);
                query.eq("module_id", insuranceTicketDto.getInsuranceTicketId());
                long count = commentService.count(query);
                insuranceTicketDto.setCommentCount(count> 0 ? count : 0);

                QueryWrapper<Follower> followerQueryWrapper = Wrappers.query();
                followerQueryWrapper.eq("user_id",userId);
                followerQueryWrapper.eq("fans_id", insuranceTicketDto.getUserId());
                Follower follower = followerMapper.selectOne(followerQueryWrapper);
                insuranceTicketDto.setIs_followedByUser(follower==null ? false :follower.getStatus());
                return  R.success(insuranceTicketDto, "查询成功");
            } else {
                OthersTicket othersTicketDto = showTicketMapper.selectOthersTicketById(id, userId);
                if(othersTicketDto==null) return R.error("晒单id不正确！", 400);
                if(f){
                    //每访问一下接口，其实浏览量就增加一次
                    UpdateWrapper<OthersTicket> wrapper = Wrappers.update();
                    wrapper.set("pv", othersTicketDto.getPv() + 1);
                    wrapper.eq("others_ticket_id", id);
                    othersTicketMapper.update(null, wrapper);
                    othersTicketDto = showTicketMapper.selectOthersTicketById(id, userId);
                }

                List<String> strings = commonHandle.imageByStringToList(othersTicketDto.getPath());// 设置图片列表
                othersTicketDto.setImages(strings);
                QueryWrapper<Comment> query = Wrappers.query();
                query.eq("module_status",status);
                query.eq("module_id", othersTicketDto.getOthersTicketId());
                long count = commentService.count(query);
                othersTicketDto.setCommentCount(count> 0 ? count : 0);

                QueryWrapper<Follower> followerQueryWrapper = Wrappers.query();
                followerQueryWrapper.eq("user_id",userId);
                followerQueryWrapper.eq("fans_id", othersTicketDto.getUserId());
                Follower follower = followerMapper.selectOne(followerQueryWrapper);
                othersTicketDto.setIs_followedByUser(follower==null ? false :follower.getStatus());
                return R.success(othersTicketDto, "查询成功");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public <T> R<List<T>> showAllTicketByUserId() {
        try {
            Map<String, Object> map = ThreadLocalUtil.get();
            Integer userId = (Integer) map.get("id");
            List<T> tickets = new ArrayList<>();

            List<AirTicket> airTicket = showTicketMapper.selectAirTicketByUserId(userId);
            List<HotelTicket> hotelTicket = showTicketMapper.selectHotelTicketByUserId(userId);
            List<InsuranceTicket> insuranceTicket = showTicketMapper.selectInsuranceTicketByUserId(userId);
            List<OthersTicket> othersTicket = showTicketMapper.selectOthersTicketByUserId(userId);

            //将字符串图片地址转换为数组
            for (AirTicket myTime : airTicket) {
                List<String> strings = commonHandle.imageByStringToList(myTime.getPath());
                myTime.setImages(strings);
                QueryWrapper<Comment> query = Wrappers.query();
                query.eq("module_status",myTime.getStatus());
                query.eq("module_id", myTime.getAirTicketId());
                long count = commentService.count(query);
                myTime.setCommentCount(count> 0 ? count : 0);
            }
            for (HotelTicket myTime : hotelTicket) {
                List<String> strings = commonHandle.imageByStringToList(myTime.getPath());
                myTime.setImages(strings);
                QueryWrapper<Comment> query = Wrappers.query();
                query.eq("module_status",myTime.getStatus());
                query.eq("module_id", myTime.getHotelTicketId());
                long count = commentService.count(query);
                myTime.setCommentCount(count> 0 ? count : 0);
            }
            for (InsuranceTicket myTime : insuranceTicket) {
                List<String> strings = commonHandle.imageByStringToList(myTime.getPath());
                myTime.setImages(strings);
                QueryWrapper<Comment> query = Wrappers.query();
                query.eq("module_status",myTime.getStatus());
                query.eq("module_id", myTime.getInsuranceTicketId());
                long count = commentService.count(query);
                myTime.setCommentCount(count> 0 ? count : 0);
            }
            for (OthersTicket myTime : othersTicket) {
                List<String> strings = commonHandle.imageByStringToList(myTime.getPath());
                myTime.setImages(strings);
                QueryWrapper<Comment> query = Wrappers.query();
                query.eq("module_status",myTime.getStatus());
                query.eq("module_id", myTime.getOthersTicketId());
                long count = commentService.count(query);
                myTime.setCommentCount(count> 0 ? count : 0);
            }

            Map<String, List<T>> mapTicket = new HashMap<>();
            mapTicket.put("AirTicket", (List<T>) airTicket);
            mapTicket.put("HotelTicket", (List<T>) hotelTicket);
            mapTicket.put("InsuranceTicket", (List<T>) insuranceTicket);
            mapTicket.put("OthersTicket", (List<T>) othersTicket);
            tickets.add((T) mapTicket);
            return R.success(tickets, "查询用户所有晒单成功");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
